#!/usr/bin/env perl 
#
# $Id: check_netdevice.pl 85286152877b 2012/11/10 04:29:10 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#       

use strict;
use warnings;
use Getopt::Long;
use Net::SNMP;

Getopt::Long::Configure("default", "bundling");
push my @prog, split(/\//, $0);
my $program = $prog[$#prog];

my $arg = int(scalar(@ARGV));
my %opt = ();
my $session = undef;
my $error = undef;

sub sighandle {
   die "$program got interrupt signal, terminating the application\n";
}

sub errx {
   my ($state, $message) = @_;

   if ($state eq -1) {
      printf("UNKNOWN: %s\n", $message);
   }
   elsif ($state eq 0) {
      printf("OK");
   }
   elsif ($state eq 1) {
      printf("WARNING: %s\n", $message);
   }
   elsif ($state eq 2) {
      printf("CRITICAL: %s\n", $message);
   }

   if (defined($session)) {
      $session->close;
   }

   exit($state);
}

sub service_check {
   my ($monitor, $value) = @_;

   if ($monitor == "memory") {
      my $freemem = $session->get_request(-varbindlist => [$oid]);
      my $free = $freemem->{$oid};

      if ($free lt 75) {
         errx(0, "memory is okay");
      }
      elsif (($free lt 85) && ($free gt 75)) {
         errx(1, "memory too high");
      }
      elsif ($free gt 85) {
         errx(2, "memory is critical");
      }
      elsif ($free lt 0) {
         errx(-1, "memory query error");
      }
   }
   elsif ($monitor == "cpu") {
      my $cpufree = $session->get_request(-varbindlist => [$oid]);
      my $free = $cpufree->{$oid};

      if ($free lt 0) {
         errx(-1, "cpu query error");
      }
      elsif ($free lt 75) {
         errx(0, "cpu is okay");
      }
      elsif (($free lt 85) && ($free gt 75)){
         errx(1, "cpu too high");
      }
      elsif ($free gt 85) {
         errx(2, "cpu is critical");
      }
   }
}

sub usage {
   printf("%s: options\n", $program);
   print "\t--help | -?         [ prints this help ]\n";
   print "\t--type | -t         [ type of device ]\n";
   print "\t--monitor | -m       [ type of nagios check ]\n";
   print "\t--host | -h         [ hostname of the device ]\n";
   print "\t--community | -c    [ SNMP v2/v1 community for the device ]\n";
   print "\n";
   print "\tList of options for monitor switch\n";
   print "\t\tmemory\n";
   print "\t\tcpu\n";
   exit (1);
}

sub main {
   GetOption(\%opt, "help|?", "community|c=s", "host|h=s",
      "monitor|m=s", "type|t=s") or usage();
   usage() if $opt{help};

   if ($opt{host} && $opt{community}) {
      $SIG{INT} = \&sighandle;
      $SIG{TERM} = \&sighandle;

      my $result = gethostbyname($opt{host});
      if ($result) {
         my ($session, $error) = Net::SNMP->session(
            -hostname => $opt{host},
            -community => $opt{community});

         unless ($session) {
            errx(-1, "SNMP connection failed to $opt{host}");
         }

         my $devtype = undef;
         if (defined($opt{type})) {
            $devtype = lc($opt{type});
         }
         else {
            errx(-1, "no device type defined!");
         }

         my $monitor = undef;
         if (defined($opt{monitor})) {
            $monitor = lc($opt{monitor});
         }
         else {
            errx(-1, "no nagios monitor defined!");
         }

         if ($monitor == "memory") {
            my $memoid = undef;
            if ($devtype == "asa") {
               $memoid = "1.3.6.1.4.1.9.9.48.1.1.1.6.1";
            }
            elsif ($devtype == "ios") {
               $memoid = "1.3.6.1.4.1.9.9.48.1.1.1.6.1";
            }
            elsif ($devtype == "juniper") {
               $memoid = "1.3.6.1.4.1.12532.11.0";
            }
            elsif ($devtype == "f5") {
               my $totalMemOid = "1.3.6.1.4.1.3375.2.1.1.2.1.44";
               my $totalMem = $session->get_request(
                  -varbindlist => [$totalMemOid]);
               my $total = $totalMem->{$totalMemOid};

               my $usedMemOid = "1.3.6.1.4.1.3375.2.1.1.2.1.45";
               my $usedMem = $session->get_request(
                  -varbindlist => [$usedMem]);
               my $used = $usedMem->{$usedMemOid};

               my $free = ($used / $total) * 100;
               if ($free lt 75) {
                  errx(0, "memory is okay");
               }
               elsif (($free lt 85) && ($free gt 75)) {
                  errx(1, "memory too high");
               }
               elsif ($free gt 85) {
                  errx(2, "memory is critical");
               }
               elsif ($free lt 0) {
                  errx(-1, "memory query error");
               }
               # $memoid = "1.3.6.1.4.1.2021.4.11.0";
            }

            if (defined($memoid)) {
               service_check($session, $monitor, $memoid);
            }
         }
         elsif ($monitor == "cpu") {
            my $cpuoid = undef;
            # 1.3.6.1.4.1.12532.10.0
            if ($devtype == "asa") {
               $cpuoid = "1.3.6.1.4.1.9.9.109.1.1.1.1.3.1";
            }
            elsif ($devtype == "ios") {
               $cpuoid = "1.3.6.1.4.1.9.9.109.1.1.1.1.8";
            }
            elsif ($devtype == "juniper") {
               $cpuoid = "1.3.6.1.4.1.12532.10.0";
            }

            if (defined($cpuoid)) {
               service_check($session, $monitor, $cpuoid);
            }
         }
      }
      else {
         errx(-1, "cannot resolve $opt{host} hostname");
      }
   }
   else {
      errx(-1, "hostname and community are required parameters");
   }
}

if ($arg le 1) {
   usage();
}
else {
   main();
}

